//
//  ViewController.swift
//  UIImageViewDemo
//
//  Created by itshizhan on 2020/10/24.
//  Copyright © 2020 itshizhan. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

	// 背景颜色作为图片
	lazy var imageView1:UIImageView = {
		let imageView = UIImageView()
		imageView.backgroundColor = UIColor.lightGray
		imageView.frame = CGRect(x: 10, y: 64, width: 200, height: 100)
		return imageView
		
	}()
	
	//  加载本地图片
	lazy var imageView2:UIImageView = {
		let imageView = UIImageView()
		imageView.backgroundColor = UIColor.lightGray
		imageView.frame = CGRect(x: 10, y: 166, width: 200, height: 100)
		imageView.image = UIImage(named: "baidu")
		
		//从文件目录中获取图片
		//let path = Bundle.main.path(forResource: "baidu", ofType:"png")
		//let fileimage = UIImage(contentsOfFile: path!)
		//imageView.image = fileimage
		
		
		//关键属性
		imageView.contentMode = .scaleAspectFit
		//边框设置
		imageView.layer.borderColor = UIColor.red.cgColor
		imageView.layer.borderWidth = 2
		
		//圆角的设置
		imageView.layer.cornerRadius = 20
		imageView.layer.masksToBounds = true
		
		//添加tap点击事件，重要
		let guester = UITapGestureRecognizer(target: self,  action: #selector(ViewController.imageClicked))
		imageView.addGestureRecognizer(guester)
		imageView.isUserInteractionEnabled = true
		
		return imageView
		
	}()
	
	//  加载网络图片
	lazy var imageView3:UIImageView = {
		let imageView = UIImageView()
		imageView.frame = CGRect(x: 10, y: 268, width: 200, height: 100)
		let url = URL(string:"https://www.baidu.com/img/PCtm_d9c8750bed0b3c7d089fa7d55720d6cf.png")
		let data = try! Data(contentsOf: url!)
		
		imageView.image = UIImage(data: data)
		//边框设置
		imageView.layer.borderColor = UIColor.red.cgColor
		imageView.layer.borderWidth = 2
		//关键属性
		imageView.contentMode = .scaleAspectFit
		
		
		return imageView
		
	}()
	
	// 图片轮播
	lazy var imageView4:UIImageView = {
		let imageView = UIImageView()
		imageView.frame = CGRect(x: 10, y: 370, width: 200, height: 100)
		
		//实现动画播放
		let image1 = UIImage(named: "baidu")
		let image2 = UIImage(named: "alibaba")
		// 设置轮播的图片
		imageView.animationImages = [image1!,image2!]
		//设置图片变换持续时间
		imageView.animationDuration = 2
		//开始动画
		imageView.startAnimating()
		//结束动画
		//imageView.stopAnimating()
		
		
		//边框设置
		imageView.layer.borderColor = UIColor.red.cgColor
		imageView.layer.borderWidth = 2
		//关键属性
		imageView.contentMode = .scaleAspectFit
		
		
		return imageView
		
	}()
	
	
	
	
	override func viewDidLoad() {
		super.viewDidLoad()
		view.addSubview(imageView1)
		view.addSubview(imageView2)
		view.addSubview(imageView3)
		view.addSubview(imageView4)
		
	}
	
	//点击事件处理
	@objc func imageClicked(){
		print("Logo 点击了……………")
	}
	
}


